package cn.devzyh.controller;

import cn.devzyh.domain.ResourceCategory;
import cn.devzyh.domain.ResultVo;
import cn.devzyh.service.ResourceCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/resourceCategory")
public class ResourceCategoryController {

    @Autowired
    private ResourceCategoryService resourceCategoryService;

    // 查询资源分类信息
    @GetMapping("/findAllResourceCategory")
    public ResultVo findAllResourceCategory() {

        List<ResourceCategory> list = resourceCategoryService.findAll();

        return new ResultVo(true, 200, "响应成功", list);
    }

    // 添加&修改资源分类
    @PostMapping("/saveOrUpdateResourceCategory")
    public ResultVo saveOrUpdateResourceCategory(@RequestBody ResourceCategory resourceCategory) {

        ResultVo res = new ResultVo(true, 200, "", null);

        if (null == resourceCategory.getId()) {

            resourceCategoryService.saveResourceCategory(resourceCategory);
            res.setMessage("新增资源分类信息成功");
        } else {

            resourceCategoryService.updateResourceCategory(resourceCategory);
            res.setMessage("修改资源分类信息成功");
        }

        return res;
    }

    // 删除资源分类
    @GetMapping("/deleteResourceCategory")
    public ResultVo deleteResourceCategory(@RequestParam(value = "id", required = true) Integer id) {

        resourceCategoryService.deleteResourceCategory(id);

        return new ResultVo(true, 200, "删除资源分类信息成功", null);
    }

}
